package com.fudan.backend.dao;

import com.fudan.backend.handler.IdTypeHandler;
import com.fudan.backend.pojo.Address;
import com.fudan.backend.pojo.DeliveryInfo;
import com.fudan.backend.pojo.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DeliveryInfoDao {
    @Results(id = "deliveryInfoResultMap", value = {
            @Result(column = "delivery_info_id", property = "deliveryInfoId", typeHandler = IdTypeHandler.class),
            @Result(column = "store_id", property = "storeId", typeHandler = IdTypeHandler.class),
            @Result(column = "order_id", property = "orderId", typeHandler = IdTypeHandler.class),
            @Result(column = "address_id", property = "address", typeHandler = IdTypeHandler.class, javaType = Address.class,
            one = @One(select = "com.fudan.backend.dao.AddressDao.getAddressByAddressId")),
            @Result(column = "order_id", property = "order", typeHandler = IdTypeHandler.class, javaType = Order.class,
            one = @One(select = "com.fudan.backend.dao.OrderDao.selectOrderByOrderIdNotCheckDelete"))
    })
    @Select("select * from t_delivery_info where delivery_info_id = #{id}")
    DeliveryInfo getDeliveryInfoByDeliveryInfoId(String id);

    @ResultMap("deliveryInfoResultMap")
    @Select("select * from t_delivery_info where store_id = #{storeId}")
    List<DeliveryInfo> getDeliveryInfoByStoreId(String storeId);

    @Insert("insert into t_delivery_info values " +
            "(#{deliveryInfoId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{orderId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{addressId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{status})")
    Integer addDeliveryInfo(DeliveryInfo deliveryInfo);
    @Update("update t_delivery_info set " +
            "store_id = #{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "order_id = #{orderId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "address_id = #{addressId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "status = #{status} " +
            "where delivery_info_id = #{deliveryInfoId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    Integer updateDeliveryInfo(DeliveryInfo deliveryInfo);
    @Update("update t_delivery_info set " +
            "status = #{status} " +
            "where delivery_info_id = #{deliveryInfoId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    Integer setDeliveryInfoStatus(String deliveryInfoId, int status);
}

